package com.sunny.l74;

public class Solution {

    public static void main(String[] args) {

        Solution solution = new Solution();
        int[] A = {1,1,2};
        solution.sortColors(A);
        for(int i=0;i<A.length;i++){
            System.out.print(A[i]+" ");
        }
        System.out.println();

    }

    public void sortColors(int[] A) {
        if(null == A)
            return;
        int len = A.length;
        int zeroIndex = 0;
        int twoIndex = len-1;
        for(int i=0;i<=twoIndex;){
            if(zeroIndex > twoIndex)
                break;
            if(0 == A[i]){
                swap(A, zeroIndex++, i++);
            }else if(1 == A[i]){
                i++;
            }else{
                swap(A, twoIndex--, i);
            }
        }
    }
    public void swap(int[] A, int i, int j){
        int tmp = A[i];
        A[i] = A[j];
        A[j] = tmp;
    }
}